package com.wpp.lagouserviceuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wpp.lagouserviceuser.convert.UserConvert;
import com.wpp.lagouserviceuser.dao.UserDao;
import com.wpp.lagouserviceuser.param.UserParam;
import com.wpp.lagouserviceuser.pojo.UserPO;
import com.wpp.lagouserviceuser.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author wpp25
 * @date 2021/5/13 21:39
 * @description：
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public UserPO queryByUserName(String userName) {
        UserPO userPO =new UserPO();

        userPO.setUserName(userName);
        Wrapper<UserPO> wrapper = new LambdaQueryWrapper<UserPO>()
                .eq(UserPO::getUserName,userName);
        return userDao.selectOne(wrapper);
    }

    @Override
    public boolean register(UserParam user) {
        UserPO userPO = queryByUserName(user.getUserName());
        if (Objects.nonNull(userPO)) {
            return false;
        }

        int insert = userDao.insert(UserConvert.convert(user));

        return insert > 0;
    }
}
